package com.sxjh.controller.ny;


import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxjh.annotation.UserLog;
import com.sxjh.common.BaseController;
import com.sxjh.common.Result;
import com.sxjh.common.ZPage;
import com.sxjh.config.Authorization;
import com.sxjh.entity.VehicleType;
import com.sxjh.entity.WagonNumber;
import com.sxjh.entity.param.VehicleTypeAddParam;
import com.sxjh.entity.param.VehicleTypeListParam;
import com.sxjh.entity.param.VehicleTypeUpdateParam;
import com.sxjh.entity.result.VehicleTypeListResult;
import com.sxjh.service.VehicleTypeService;
import com.sxjh.service.WagonNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 车辆类型  前端控制器
 * </p>
 *
 * @author lwj
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/vehicle-type")
@Api(tags = "车辆类型")
public class VehicleTypeController extends BaseController {

  @Autowired
  private VehicleTypeService vehicleTypeService;

  @Autowired
  private WagonNumberService wagonNumberService;

  @ApiOperation("列表")
  @GetMapping
  @Authorization("vehicleType.list")
  Result<ZPage<VehicleTypeListResult>> list(@Validated VehicleTypeListParam param) {
    LambdaQueryWrapper<VehicleType> wrapper = new LambdaQueryWrapper<>();
    // 根据类型名称搜索
    if (StrUtil.isNotEmpty(param.getName())) wrapper.like(VehicleType::getName, param.getName());
    if (StrUtil.isNotEmpty(param.getDisable())) wrapper.eq(VehicleType::getDisable, BooleanUtil.toBoolean(param.getDisable()));
//    wrapper.eq(VehicleType::getDisable, false);
    wrapper.orderByDesc(VehicleType::getId);
    IPage iPage = getIPage(param);
    iPage = vehicleTypeService.page(iPage, wrapper);
    ZPage<VehicleTypeListResult> zPage = copyPage(iPage, VehicleTypeListResult::new);
    return Result.ok(zPage);
  }

  @ApiOperation("获取拉下框数据")
  @GetMapping("/select")
  Result<List<VehicleTypeListResult>> select() {
    List<VehicleType> vehicleTypeList = vehicleTypeService.list();
    List<VehicleTypeListResult> results = copyList(vehicleTypeList, VehicleTypeListResult::new);
    return Result.ok(results);
  }

  @ApiOperation("新增")
  @PostMapping
  @Authorization("vehicleType.add")
  @UserLog(desc = "内部倒运-新增车辆管理类型", roleCode = "vehicleType.add")
  Result<String> add(@RequestBody @Validated VehicleTypeAddParam param) {
    String record = String.format("类型名称：%s", param.getName());
    return vehicleTypeService.saveInfo(param) ? Result.ok(record) : Result.err("保存失败");
  }

  @ApiOperation("更新")
  @PostMapping("/update")
  @Authorization("vehicleType.update")
  @UserLog(desc = "内部倒运-编辑车辆管理类型", roleCode = "vehicleType.update")
  Result<String> update(@RequestBody @Validated VehicleTypeUpdateParam param) {
    LambdaQueryWrapper<VehicleType> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(VehicleType::getId, param.getId());
    VehicleType updateEntity = vehicleTypeService.getBaseMapper().selectOne(wrapper);
    CglibUtil.copy(param, updateEntity);
    String record = String.format("类型名称：%s", param.getName());
    return vehicleTypeService.updateById(updateEntity) ? Result.ok(record) : Result.err("更新失败");
  }

  @ApiOperation("更新禁用状态")
  @PostMapping("/disable")
  @Authorization("vehicleType.update")
  @UserLog(desc = "内部倒运-更新车辆管理类型禁用状态", roleCode = "vehicleType.update")
  Result<String> updateDisable(@RequestBody VehicleTypeUpdateParam param) {
    LambdaQueryWrapper<VehicleType> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(VehicleType::getId, param.getId());
    VehicleType updateEntity = vehicleTypeService.getBaseMapper().selectOne(wrapper);
    updateEntity.setDisable(param.getDisable());
    updateEntity.setId(param.getId());
    String record = String.format("类型名称：%s，状态：%s", updateEntity.getName(), param.getDisable() ? "禁用" :  "启用");
    return vehicleTypeService.updateById(updateEntity) ? Result.ok(record) : Result.err("更新失败");
  }

  @ApiOperation("删除")
  @GetMapping("/delete/{id}")
  @Authorization("vehicleType.del")
  @UserLog(desc = "内部倒运-删除车辆管理类型", roleCode = "vehicleType.del")
  Result<String> del(@PathVariable Integer id) {
    int count = wagonNumberService.count(new LambdaQueryWrapper<WagonNumber>().eq(WagonNumber::getType, id));
    if (count > 0) return Result.err("车辆类型在车辆管理中被使用了");
    VehicleType updateEntity = vehicleTypeService.getById(id);
    String record = String.format("类型名称：%s", updateEntity.getName());
    return vehicleTypeService.removeById(id) ? Result.ok(record) : Result.err("删除失败,此ID不存在或其他异常");
  }

}